package com.zcm.service.weibo.serviceprovider.sina.connect;

import org.apache.commons.lang.StringUtils;
import org.springframework.social.connect.ApiAdapter;
import org.springframework.social.connect.ConnectionValues;
import org.springframework.social.connect.UserProfile;

import com.zcm.service.weibo.serviceprovider.sina.api.SinaWeibo;
import com.zcm.service.weibo.serviceprovider.sina.api.model.SinaWeiboProfile;

/**
 * @author xiongj
 * 
 */
public class SinaWeiboAdapter implements ApiAdapter<SinaWeibo> {

	public UserProfile fetchUserProfile(SinaWeibo sinaWeibo) {
		return null;
	}

	public void setConnectionValues(SinaWeibo sinaWeibo, ConnectionValues values) {
		SinaWeiboProfile profile = sinaWeibo.userOperations().getUserProfile();
		values.setProviderUserId(String.valueOf(profile.getId()));
		values.setDisplayName(profile.getName());
		values.setImageUrl(profile.getImageUrl());
		values.setProfileUrl("http://weibo.com/"
				+ StringUtils.defaultString(profile.getDomain(),
						String.valueOf(profile.getId())));
	}

	public boolean test(SinaWeibo sinaWeibo) {
		try {
			sinaWeibo.userOperations().getUserProfile();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void updateStatus(SinaWeibo sinaWeibo, String message) {
		//sinaWeibo.timelineOperations().updateStatus(message);
	}
}
